
from models.Base import SessionLocal
from models.Base import SensorData

from datetime import datetime

# 添加一条数据


def get_datetime_from_timestamp(timestamp=None):
    from datetime import datetime as dt
    if not timestamp:
        timestamp = int(dt.now().timestamp())
    return dt.fromtimestamp(int(timestamp))


def create_data_one():
    import random
    random_float = round(random.randint(0, 100), 2)
    random_float2 = round(random.randint(0, 100), 2)
    random_float3 = round(random.randint(0, 100), 2)
    location_index = "location_index_" + str(random_float)
    with SessionLocal() as session:
        p1 = SensorData(**{
            "time": get_datetime_from_timestamp(),
            "location": location_index,
            "temperature": random_float,
            "humidity": random_float2,
            "pressure": random_float3
        })
        session.add(p1)
        session.commit()


def create_data_batch(n):
    with SessionLocal() as session:
        item_list = []
        for item in range(n):
            import random
            random_float = round(random.randint(0, 100+n), 2)
            random_float2 = round(random.randint(0, 100+n), 2)
            random_float3 = round(random.randint(0, 100+n), 2)
            location_index = "location_index_" + str(random_float)
            p1 = SensorData(**{
                "time": get_datetime_from_timestamp(),
                "location": location_index,
                "temperature": random_float,
                "humidity": random_float2,
                "pressure": random_float3
            })
            item_list.append(p1)
        session.add_all(item_list)
        session.commit()


create_data_batch(n=130000)
